
def demo1(func):
    def cc(*args, **kwargs):
        print('before')
        result = func(*args, **kwargs)
        print('after')
        return result
    return cc

@demo1
def test_func():
    print('inside')

# test_func()

class ItDemo:
    def __init__(self, data) -> None:
        self.data = data
        self.idx = 0
    
    def __iter__(self):
        return self
    
    def __next__(self):
        if self.idx < len(self.data):
            res = self.data[self.idx]
            self.idx += 1
            return res
        else:
            raise StopIteration
        
# test_arr = [1,2,3,4,5]
# it = ItDemo(test_arr)
# for i in it:
#     print(i)

def yield_demo(n):
    for i in range(n):
        if i < 5:
            yield i
        else:
            return

print(yield_demo(10))
print(type([2,3,4]))
# for i in yield_demo(10):
#     print(i)

import time

def log_time(func):  # 接受一个函数作为参数
    def _log(*args, **kwargs):
        beg = time.time()
        res = func(*args, **kwargs)
        print('use time: {}'.format(time.time() - beg))
        return res

    return _log

@log_time  # 装饰器语法糖
def mysleep():
    time.sleep(1)

mysleep()

# 另一种写法，和上面的调用方式等价
def mysleep2():
    time.sleep(1)

newsleep = log_time(mysleep2)
newsleep()
